<template>
    <el-container>
        <el-main class="wrapper">
            <v-breadcrumb v-bind:breadcumb="breadcumb"></v-breadcrumb>
            <section class="panel" :style="{height: mainHeight+'px'}">
                <div class="panel-body">
                    <div class="btn-group" style="margin-bottom: 15px">
                        <el-button @click="create" type="primary" icon="fa fa-plus"
                                   size="small"> 新增
                        </el-button>
                        <el-button @click="list" type="primary" icon="fa fa-refresh"
                                   size="small"> 刷新
                        </el-button>
                    </div>
                    <el-table  v-loading="loading"
                               element-loading-text="努力加载中..."
                               element-loading-spinner="el-icon-loading"
                               :data="tableData" :border="true" style="width: 100%">
                        <el-table-column prop="id" label="编号" width="80px"></el-table-column>
                        <el-table-column prop="openid" label="账号" width="200px"></el-table-column>
                        <el-table-column prop="username" label="名称" width="200px"></el-table-column>
                        <el-table-column prop="register_at" label="注册时间" width="150px">
                            <template slot-scope="scope">{{ scope.row.register_at ? moment(scope.row.register_at).format('YYYY-MM-DD HH:mm') : ''}}</template>
                        </el-table-column>
                        <el-table-column prop="login_at" label="最近登录" width="150px">
                            <template slot-scope="scope">{{ scope.row.login_at ? moment(scope.row.login_at).format('YYYY-MM-DD HH:mm') : ''}}</template>
                        </el-table-column>
                        <el-table-column label="操作" min-width="260px">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="edit(scope.row)" icon="fa fa-edit" plain>
                                    编辑</el-button>
                                <el-button icon="fa fa-trash" v-if="scope.row.id > 1"
                                           @click="destory(scope.row)" type="danger" size="mini" plain> 删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </section>
        </el-main>
        <!-- 编辑用户，不用创建用户 -->
        <el-dialog
                title="用户编辑"
                custom-class="bh-dialog"
                top="23vh"
                @open="resetForm('userForm')"
                :visible.sync="updateUserDialog"
                width="500px">
            <el-form :model="userForm" :rules="rules" ref="userForm" label-width="80px" class="demo-ruleForm">
                <el-form-item required label="账号：" prop="openid">
                    <el-input  v-model="userForm.openid" placeholder="请填写用户账号"></el-input>
                </el-form-item>
                <el-form-item required label="名称：" prop="username">
                    <el-input v-model="userForm.username" placeholder="请填写用户名称"></el-input>
                </el-form-item>
                <el-form-item label="密码：" prop="password">
                    <el-input type="password" v-model="userForm.password" placeholder="用户密码,默认123456"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="updateUserDialog = false">取 消</el-button>
                <el-button type="primary" @click="update('userForm')">确 定</el-button>
              </span>
        </el-dialog>
    </el-container>
</template>
<script>
    import {mapGetters} from 'vuex';
    import vBreadcrumb from '../layout/breadcrumb.vue';
    import {ManageList,ManageDestory,ManageUpdate} from '../../utils/axios';
    export default {
        components:{vBreadcrumb},
        data(){
            return {
                breadcumb:[{name: "管理人员"},],
                tableData:[],//列表数据
                pageData:[],//分页数据
                updateUserDialog:false,
                search: {page:null},//筛选
                //用户表单
                userForm:{
                    id:0,
                    openid:'',
                    username:'',
                    password:'',
                },
                rules: {
                    openid: [{ required: true, message: '请填写用户账号', trigger: 'blur' }],
                    username: [{ required: true, message: '请填写用户名称', trigger: 'blur' }],
                },
            }
        },
        activated(){
            this.$store.dispatch('setActive', '/manage');
            this.list();
        },
        computed: {
            ...mapGetters(['active','mainHeight','loading'])
        },
        methods:{
            //分页
            handleCurrentChange(val) {
                this.search.page = val;
                this.list();
            },
            //获取列表
            list(){
                this.$store.dispatch('setLoading', true);
                ManageList({params: this.search}).then((response)=>{
                    if (response.code) {
                        this.$message.error(response.msg);
                    } else {
                        this.$store.dispatch('setLoading', false);
                        this.tableData = response.data;
                    }
                })
            },
            //编辑
            create(){
                this.userForm = {
                    id:0,
                    openid:'',
                    username:'',
                    password:'',
                },
                this.updateUserDialog = true;
            },
            //编辑
            edit(info){
                this.userForm = {
                    id:info.id,
                    openid: info.openid,
                    username:info.username,
                    password:''
                }
                this.updateUserDialog = true;
            },
            //更新
            update(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        ManageUpdate(this.userForm).then((response)=>{
                            if(response.code){
                                this.$message.error(response.msg);
                            }else{
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: response.msg,
                                    onClose:() =>{
                                        this.updateUserDialog = false;
                                        this.list();
                                    }
                                });
                            }
                        });
                    }
                });
            },
            //删除
            destory(info){
                this.$confirm('确定要删除"'+info.username+'"用户信息吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    ManageDestory({params:{id: info.id}}).then((response) => {
                        if (response.code) {
                            this.$message.error(response.msg);
                        } else {
                            this.list();
                        }
                    });
                }).catch(() => {});
            },
            //重置表单
            resetForm(formName) {
                if (this.$refs[formName]!==undefined) {
                    this.$refs[formName].resetFields();
                }
            },
        },
    }
</script>